package com.allwees.bs.c.module.product.dto;

import com.allwees.bs.c.module.product.entity.SkuEntity;
import com.allwees.core.common.constant.EStatus;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Daniel
 * @Version 1.0.0
 * @Description
 * @CreateTime 2020/10/14 15:39
 */
@Setter
@Getter
public class SkuCheckResult implements Serializable {

    private static final long serialVersionUID = 6427672182443818681L;

    private boolean success;

    private Map<String,CheckResult> checkResult;

    public static SkuCheckResult success(){
        SkuCheckResult result = new SkuCheckResult();
        result.setSuccess(true);
        return result;
    }

    public static SkuCheckResult of(List<SkuEntity> list){
        SkuCheckResult skuCheckResult = new SkuCheckResult();
        Map<String,CheckResult> checkResult = new HashMap<>();
        boolean success = true;

        CheckResult result;
        for (SkuEntity sku : list) {
            result = new CheckResult(sku);
            checkResult.put(result.skuUuid, result);
            if(result.status != SkuStatus.NORMAL){
                success = false;
            }
        }
        skuCheckResult.setCheckResult(checkResult);
        skuCheckResult.setSuccess(success);
        return skuCheckResult;
    }


    @Getter
    @Setter
    public static class CheckResult{
        private String skuUuid;

        private SkuStatus status;

        public CheckResult(SkuEntity sku){
            this.skuUuid = sku.getUuid();
            this.status = getMappedStatus(sku);
        }

        private SkuStatus getMappedStatus(SkuEntity sku){
            if(!EStatus.NORMAL.value().equals(sku.getStatus())){
                return SkuStatus.INVALID;
            }

            if(sku.getQuantity() <= 0){
                return SkuStatus.SOLDOUT;
            }

            if(sku.getQuantity() < sku.getBuyQuantity()){
                return SkuStatus.NOTENOUGH;
            }

            return SkuStatus.NORMAL;
        }
    }

    public enum SkuStatus{
        /**
         * 正常
         */
        NORMAL,
        /**
         * 缺货
         */
        SOLDOUT,
        /**
         * 库存不足
         */
        NOTENOUGH,
        /**
         * 商品状态不可用
         */
        INVALID
    }
}
